package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysFlowCategoryMapper;
import com.ruoyi.common.core.domain.entity.SysFlowCategory;
import com.ruoyi.system.service.ISysFlowCategoryService;

/**
 * 流程分类Service业务层处理
 *
 * @author Tony
 * @date 2023-04-07
 */
@Service
public class SysFlowCategoryServiceImpl implements ISysFlowCategoryService {
    @Autowired
    private SysFlowCategoryMapper sysFlowCategoryMapper;

    /**
     * 查询流程分类
     *
     * @param id 流程分类主键
     * @return 流程分类
     */
    @Override
    public SysFlowCategory selectSysFlowCategoryById(Long id) {
        return sysFlowCategoryMapper.selectSysFlowCategoryById(id);
    }

    /**
     * 查询流程分类列表
     *
     * @param sysFlowCategory 流程分类
     * @return 流程分类
     */
    @Override
    public List<SysFlowCategory> selectSysFlowCategoryList(SysFlowCategory sysFlowCategory) {
        return sysFlowCategoryMapper.selectSysFlowCategoryList(sysFlowCategory);
    }

    /**
     * 新增流程分类
     *
     * @param sysFlowCategory 流程分类
     * @return 结果
     */
    @Override
    public int insertSysFlowCategory(SysFlowCategory sysFlowCategory) {
        sysFlowCategory.setCreateTime(DateUtils.getNowDate());
        return sysFlowCategoryMapper.insertSysFlowCategory(sysFlowCategory);
    }

    /**
     * 修改流程分类
     *
     * @param sysFlowCategory 流程分类
     * @return 结果
     */
    @Override
    public int updateSysFlowCategory(SysFlowCategory sysFlowCategory) {
        sysFlowCategory.setUpdateTime(DateUtils.getNowDate());
        return sysFlowCategoryMapper.updateSysFlowCategory(sysFlowCategory);
    }

    /**
     * 批量删除流程分类
     *
     * @param ids 需要删除的流程分类主键
     * @return 结果
     */
    @Override
    public int deleteSysFlowCategoryByIds(Long[] ids) {
        return sysFlowCategoryMapper.deleteSysFlowCategoryByIds(ids);
    }

    /**
     * 删除流程分类信息
     *
     * @param id 流程分类主键
     * @return 结果
     */
    @Override
    public int deleteSysFlowCategoryById(Long id) {
        return sysFlowCategoryMapper.deleteSysFlowCategoryById(id);
    }

    /**
     * 获取流程分类树列表
     *
     * @param flowCategory 流程信息
     * @return 结果
     */
    @Override
    public List<TreeSelect> selectCategoryTreeList(SysFlowCategory flowCategory) {
        List<SysFlowCategory> categoryList = sysFlowCategoryMapper.selectSysFlowCategoryList(flowCategory);
        return buildCategoryTreeSelect(categoryList);
    }

    /**
     * 构建树形机构数据
     * @param categoryList
     * @return
     */
    public List<TreeSelect> buildCategoryTreeSelect(List<SysFlowCategory> categoryList) {
        List<SysFlowCategory> categoryTrees = buildCategoryTree(categoryList);
        return categoryTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param categoryList 分类列表
     * @return 树结构列表
     */
    public List<SysFlowCategory> buildCategoryTree(List<SysFlowCategory> categoryList) {
        List<SysFlowCategory> returnList = new ArrayList<>();
        List<Long> tempList = categoryList.stream().map(SysFlowCategory::getId).collect(Collectors.toList());
        for (SysFlowCategory category : categoryList) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId())) {
                recursionFn(categoryList, category);
                returnList.add(category);
            }
        }
        if (returnList.isEmpty()) {
            returnList = categoryList;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysFlowCategory> list, SysFlowCategory t) {
        // 得到子节点列表
        List<SysFlowCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysFlowCategory tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysFlowCategory> getChildList(List<SysFlowCategory> list, SysFlowCategory t) {
        List<SysFlowCategory> tlist = new ArrayList<>();
        for (SysFlowCategory n : list) {
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysFlowCategory> list, SysFlowCategory t) {
        return getChildList(list, t).size() > 0;
    }
}
